<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <input type="text" v-model="searchname">
        <ul>
            <li v-for="(item, index) in searchedArr" :key="index">{{index}}----{{item}}</li>
        </ul>
        <button @click="order(1)">升序</button>
        <button @click="order(2)">降序</button>
        <button @click="order(0)">原始</button>
    </div>
    <script src="../js/vue.js"></script>
    <script>
        const vm = new Vue({
            el: '#app',
            data: {
                searchname: '',
                orderType: 0,
                arr: [
                    {name: 'tom', age: 12},
                    {name: 'jack', age: 15},
                    {name: 'tomi', age: 11},
                    {name: 'jerry', age: 13}
                ]
            },
            computed: {
                searchedArr: {
                    get(){
                        const {searchname, arr, orderType} = this
                        let newarr = arr.filter(item=>{
                            return item.name.indexOf(searchname)!==-1
                        })
                        if(orderType===1){
                            newarr.sort((p, l)=>{
                            return p.age-l.age
                        })
                        }else if(orderType===2){
                            newarr.sort((p, l)=>{
                            return l.age-p.age
                        })
                        }
                        return newarr
                    }
                }
            },
            methods: {
                order(type){
                    this.orderType = type
                }
            }
        })
    </script>
</body>
</html>